<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 10/2/18
 * Time: 6:43 PM
 */

namespace home\modules\docu\models;


use common\models\db\DocuComment;
use yii\base\Model;
use yii\db\Exception;

class FormComment extends Model
{
    public $docu_id;
    public $body;
    public $code;

    public function attributeLabels()
    {
        return [
            'body' => "内容",
            'code' => "验证码"
        ];
    }

    public function rules()
    {
        return [
            [['docu_id', 'body', 'code'], 'required'],
            ['body', 'trim'],
            ['body', 'string', 'min' => 6, 'max' => 65535],
            ['code', 'captcha'],
        ];
    }

    /**
     * @throws Exception
     */
    public function entry()
    {
        $comment = new DocuComment();
        $comment->docu_id = $this->docu_id;
        $comment->body = $this->body;
        $comment->created_at = Yii_BT_TIME;
        $comment->created_by = \Yii::$app->user->id;
        $comment->status = DocuComment::STATUS_DEFAULT;
        if (!$comment->save()){
            throw new Exception(\common\members\wodrow\tools\Model::getModelError($comment));
        }
    }

}